package com.zl.smartmall.library.im.vo;

import java.util.List;

import net.tsz.afinal.FinalDb;
import android.content.Context;

import com.zl.smartmall.library.im.po.SessionInfo;

public class SessionInfoDao {

	private FinalDb mFinalDb;
	
	public SessionInfoDao(Context context) {
		mFinalDb = FinalDb.create(context);
	}
	
	public void insert(SessionInfo model) {
		synchronized (SessionInfoDao.class) {
			mFinalDb.save(model);
		}

	}
	
	public void delete() {
		synchronized (SessionInfoDao.class) {
			mFinalDb.deleteByWhere(SessionInfo.class, null);
		}
	}
	
	public void delete(int uid) {
		synchronized (SessionInfoDao.class) {
			mFinalDb.deleteByWhere(SessionInfo.class, "uid=" + uid);
		}
	}

	public List<SessionInfo> query() {
		synchronized (SessionInfoDao.class) {
			List<SessionInfo> models = mFinalDb.findAllByWhere(
					SessionInfo.class, null, "time desc");
//			Collections.reverse(models);
			return models;
		}
	}

	public SessionInfo query(int uid) {
		synchronized (SessionInfoDao.class) {
			List<SessionInfo> models = mFinalDb.findAllByWhere(
					SessionInfo.class, "uid=" + uid);
//			Collections.reverse(models);
			if(models.size() > 0) {
				return models.get(0);
			}
			return null;
		}
	}
	
	public void update(SessionInfo model) {
		synchronized (SessionInfoDao.class) {
			mFinalDb.update(model, "uid=" + model.getUid());
		}
	}
	
}
